import maya.cmds as mc

import LZ_public_command.LZ_blendshape_command as LZ_blendshape_command
reload(LZ_blendshape_command)



class replaceMesh_connected( LZ_blendshape_command.LZ_blendshape_command 
                             ):
    def __init__(self):
        pass
        
        
    def replaceMesh_connection(self , orgMesh = '' ,  Origmesh = '' , cloneMehs = '' ):
        print(Origmesh)
        blendshape_node = self.list_BlendNode(  mesh = Origmesh )
        print('q'+blendshape_node+'p')
        targets = self.getWeightName( blendShapeNode = blendshape_node )
        ainnode = ['animCurveUU' ,'animCurveUL' ]
        
        #print(targets)
        connection_info = []
        for tg in targets:
        
            blendAttr = str(blendshape_node) + '.' +  str(tg) 
            
            
            
            connected_Attr = mc.listConnections( blendAttr , s= True ,d =False , p=True, skipConversionNodes = True )
            if connected_Attr :
                
                #if mc.nodeType( connected_Attr ) in  ainnode :
                #    
                #    sdk_sourceInfo = mc.listConnections( connected_Attr ,s =True ,d = False , p =True , skipConversionNodes = True )
                #    
                #    sdk_node = connected_Attr
                #    
                #else :
                #    sdk_sourceInfo = mc.listConnections( blendAttr , s= True ,d =False ,p=True, skipConversionNodes = True )
                    
                sdk_sourceInfo = connected_Attr
                valueAttr = 0
                #print(connected_Attr)
                mc.disconnectAttr( connected_Attr[0]  , blendAttr   )
            else:
                sdk_sourceInfo = None
                valueAttr = mc.getAttr( blendAttr )
            
            connection_info.append(  [ sdk_sourceInfo , blendshape_node , tg , valueAttr ] )
            
            #print( connection_info )
            mc.setAttr( blendAttr , 0 )
        
        all_cloneMesh = []
        for ci in connection_info :
            
            
            
            blendAttr = str(ci[1] ) + '.' + str(ci[2])
            
            targetMesh_clone = str(ci[2]) + '_clone'
            
            
            mc.setAttr( blendAttr , 1  )
            
            wrapMesh = self.LZ_WrapDeformer( originalObj = orgMesh , shapeToCopy =  Origmesh    , newObj = cloneMehs  ) 
            
            mc.rename( wrapMesh , targetMesh_clone )
            mc.setAttr( blendAttr , 0 )
            
            all_cloneMesh.append( targetMesh_clone )
        #print(targetMesh_clone)
        blendNode_clone = mc.blendShape( all_cloneMesh , cloneMehs )
        
        for ci in connection_info :
            targetMesh_clone = str(ci[2]) + '_clone'
            blendAttr = str(blendNode_clone[0]) + '.' + str(targetMesh_clone) 
            
            
            if ci[0] == None :
                mc.setAttr( blendAttr ,ci[3]   )
                
            else:
                mc.connectAttr( ci[0][0] ,  blendAttr )
           

            
            
            
        